package com.bt.service.Impl;



import com.bt.dao.UserMapper;
import com.bt.entity.User;
import com.bt.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> findByCondition(String name, String sex, String address, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize); // 分页

        User user = new User();
        if (StringUtils.hasText(name)) user.setName(name); // 无需拼接 %
        if (StringUtils.hasText(sex)) user.setSex(sex);
        if (StringUtils.hasText(address)) user.setAddress(address);

        List<User> users = userMapper.selectByCondition(user);
        return new PageInfo<>(users);
    }
    @Override
    public User login(String name, String password) {
        return userMapper.login(name, password);
    }

    @Override
    public List<User> getUserList() {
        return userMapper.selectAll();
    }

    @Override
    public User selectUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int delUser(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    @Transactional
    public int addUser(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public User findByUsername(String username) {
        User user=new User();
        user.setName(username);
        return userMapper.selectOne(user);
    }

    @Override
    @Transactional
    public int saveUser(User user) {
        return userMapper.insertSelective(user);
    }
}
